package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.IntegerConstant;
import com.itheima.reggie.common.ServerResultVO;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressMapperController {
    @Autowired
    AddressBookService addressBookService;

    @PostMapping
    public ServerResultVO<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getId());
        addressBookService.save(addressBook);
        log.info("user:{} get a new address book:{}", BaseContext.getId(), addressBook);
        return ServerResultVO.success(addressBook);
    }

    @PutMapping("/default")
    public ServerResultVO<AddressBook> putDefault(@RequestBody AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getId());
        updateWrapper.set(AddressBook::getIsDefault, IntegerConstant.DEFAULT_STATUS_FALSE);
        addressBookService.update(updateWrapper);

        addressBook.setIsDefault(IntegerConstant.DEFAULT_STATUS_TRUE);
        addressBookService.updateById(addressBook);
        log.info("user:{} get a default address book:{}", BaseContext.getId(), addressBook);
        return ServerResultVO.success(addressBook);
    }

    @GetMapping("/{id}")
    public ServerResultVO<AddressBook> get(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook == null) {
            return ServerResultVO.error("no addressBook matches id" + id.toString());
        }
        return ServerResultVO.success(addressBook);
    }

    @GetMapping("/default")
    public ServerResultVO<AddressBook> getDefault() {
        Long id = BaseContext.getId();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, id);
        queryWrapper.eq(AddressBook::getIsDefault, IntegerConstant.DEFAULT_STATUS_TRUE);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null) {
            return ServerResultVO.error("no default address matches id" + id.toString());
        }
        return ServerResultVO.success(addressBook);
    }

    @GetMapping("list")
    public ServerResultVO<List<AddressBook>> list(AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, BaseContext.getId());
        queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        return ServerResultVO.success(addressBookService.list(queryWrapper));
    }
}